package com.wmeimob.fastboot.jiubei.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.wmeimob.fastboot.aliyun.AliyunSms;
import com.wmeimob.fastboot.aliyun.SmsSendHandler;
import com.wmeimob.fastboot.aliyun.config.AliyunSmsProperties;
import com.wmeimob.fastboot.core.exception.CustomException;
import com.wmeimob.fastboot.core.rest.RestResult;
import com.wmeimob.fastboot.jiubei.entity.Member;
import com.wmeimob.fastboot.jiubei.result.RestResponse;
import com.wmeimob.fastboot.jiubei.result.RestResults;
import com.wmeimob.fastboot.jiubei.service.MemberService;
import com.wmeimob.fastboot.starter.security.JwtAuthenticationFilter;
import com.wmeimob.fastboot.util.InputValidator;
import com.wmeimob.fastboot.util.RandomCodeUtil;
import com.wmeimob.fastboot.wechat.starter.service.WechatService;
import lombok.extern.slf4j.Slf4j;
import me.hao0.wechat.core.Wechat;
import me.hao0.wechat.exception.WechatException;
import me.hao0.wechat.model.base.AuthAccessToken;
import me.hao0.wechat.model.base.AuthType;
import me.hao0.wechat.model.base.JSCode2SessionResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Meimeng
 */
@RestController
@RequestMapping("/auth")
@Slf4j
public class AuthTokenController {
    @Autowired
    private JwtAuthenticationFilter jwtAuthenticationFilter;
    @Autowired
    private WechatService wechatService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private MemberService memberService;
    @Resource
    private AliyunSmsProperties aliyunSmsProperties;


    public static final String SUBSCRIBE_LIST_KEY = "USER:SUBSCRIBE:LIST";

    @Resource
    private SmsSendHandler smsSendHandler;

    public static final String GET_WX_USER_INFO_MINI = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID" +
            "&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";

    /**
     * 公众号授权
     *
     * @param code
     * @return :
     * @author : wjt
     */
    @GetMapping("/wx-token")
    public RestResult getToken(String code) {
        /*Assert.notNull(code, "code is null");
        Wechat wechat = this.wechatService.getApiComponent();
        AuthAccessToken authAccessToken;
        try {
            authAccessToken = wechat.base().authAccessToken(code);
        } catch (WechatException var10) {
            log.error(var10.getMessage());
            throw new CustomException("网络异常，请稍候再试");
        }

        String openid = authAccessToken.getOpenId();*/
        Member member = new Member();
        member.setId(1L);
        //生成token
        String token = jwtAuthenticationFilter.getJsonWebTokenHandler().generateToken(member);
        return RestResult.msg(200, token);

    }


    @GetMapping("/reg-sms")
    public RestResults sendRegSms(String mobile) {
        String scene = "reg";
        if (!InputValidator.isMobile(mobile)) {
            throw new CustomException("不正确的手机号码");
        } else {
            String code = RandomCodeUtil.randCodeNum(
                    ((AliyunSmsProperties.SignTemplate) this.aliyunSmsProperties.getConfigs().get(scene))
                            .getLength());
            this.smsSendHandler.process(mobile, code, scene, this.aliyunSmsProperties);
            Map<String, String> paramMap = new HashMap();
            paramMap.put("code", code);

            try {
                SendSmsResponse resp = AliyunSms.getInstance(this.aliyunSmsProperties)
                        .sendSms(mobile, paramMap, scene);
                log.info(JSONObject.toJSONString(resp));
            } catch (ClientException var5) {
                log.error(var5.getMessage(), var5);
            }
        }
        return RestResponse.makeOKRsp();
    }

}
